<?php 
namespace app\socket\controller;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
use \GatewayWorker\Lib\Gateway;
use app\common\library\Game;
use think\Db;
class Wsroom
{
	public static  function onConnect($client_id=''){
		echo "\ noncon:".$client_id;
	}
	public static  function onWorkerStart($worker){
		global $game;
		$game=new Game();
		echo "\n onWorkerStart".$worker->id;
		if ($worker->id==0) {

			Timer::add(0.4, function(){
					//self::loop_game();
					});
			self::init_game();
		}

		//
	}
	//
	public static  function onClose($client_id){
		global $xredis;
		Gateway::leaveGroup($client_id, 1);
		echo "\n onClose:".$client_id;
	}
	//定時器不断查询
	public static  function onmessage($client_id, $message_data){


		$mdata=json_decode($message_data,true);
print_r($mdata);
		$msg='';
		if(!empty($mdata['irq'])){
			if($mdata['irq']==1){
				Gateway::sendToClient($client_id,'~m~'.($tou[2]+32).'~m~~j~{"err":0,"irs":1,"vals":['.$mdata['vals'][0].','.$mdata['vals'][1].',2,-202850811],"msg":null}');

				if($mdata['vals'][1]>4000 && $mdata['vals'][1]<7000){
					Gateway::sendToClient($client_id,'~m~185~m~~j~{"vals":[1,"\u5C0A\u656C\u7684\u73A9\u5BB6\uFF0C\u9009\uFF08VIP\u5145\u503C\uFF09\u901A\u9053\uFF0C\u4E13\u4EBA\u4E00\u5BF9\u4E00\u670D\u52A1\uFF0C100%\u6210\u529F\u7387"],"evt":208}');
					//
				}
			}
		}
		if(!empty($mdata['req'])){


			switch ($mdata['req']) {
				case 1:
					if($mdata['vals'][0]==0 && $mdata['vals'][1]==3){
					}else{
					}
					break;
				case 2:
					if($mdata['vals'][0]==0 && $mdata['vals'][1]==197){ }
					break;
				case 225:
					if($mdata['vals'][1]['game_code']==101){
					}

					if($mdata['vals'][1]['game_code']==102){
					}

					break;
				case 226:
					break;
				case 229:
					Gateway::sendToClient($client_id,'~m~33~m~~j~{"err":0,"res":229,"msg":null}');
					break;

				case 230:
					Gateway::sendToClient($client_id,'~m~33~m~~j~{"err":0,"res":230,"msg":null}');

					break;


				default:
# code...
					break;
			}
			if($mdata['req']==1){


			}
		}
		echo "----onmessage_data=\n";
		if($msg){
			//Gateway::sendToClient($client_id,$msg);
		}
		return;

	}
	//修复程序，数据，赔付
	//定時器不断查询
	public static  function loop_game(){
		global $xredis;
		global $game;
		$addon=json_decode($xredis->get($game->prefix.'Biquan'),true);
		$temp_set= $xredis->get($game->prefix.'_now_time');
		$now_time=time();
		self::sendtogroup(1,'loop_game');
		return ;
	} 

	//起始时间，起始值，控制涨跌0涨：type=0控制未来标涨 type=1控制未来跌 $kill
	private static function xxxxxxxxxxxxxxxxxxxxxdataxxxxxxxxxxxxxxxxxxxx(){}
	//决策弹出
	private static function yyyyyyyyyyyyyyyyyyyyyyyyfffyyyyyyyyyyyyyyyyyy(){}
	//群发信息
	public static  function sendtogroup($roomid,$data){
		if($roomid<=0){
			echo "\nempty roomid\n";
		}else{
			Gateway::sendToGroup($roomid, $data);
		}
		return;
	}
	public static  function sendto_client_id($client_id,$data){
		if($client_id!=""){
			echo "\nempty client_id\n";
		}else{
			Gateway::sendToclient($client_id, $data);
		}
		return;
	}

	private static function init_game(){
		global $game;
		global $xredis;
		$xredis=$game->xredis(1);
		self::clearn_redis();
		return 1;
	}

	//清除掉所有关于本项目的缓存
	private static function clearn_redis(){
		global $xredis;
		global $game;
		$dat=$xredis->keys('*'.$game->prefix.'*');//最后一个
		foreach ($dat as $key => $value) {
			$rel=$xredis->del($value);//删除
		}
		return;
	}

}
